package de.jmda.sample.mproc.task;

import static de.jmda.core.mproc.ProcessingUtilities.asTypeElement;
import static de.jmda.core.util.CollectionsUtil.asSet;

import java.io.IOException;
import java.net.IDN;
import java.util.Set;

import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;

import org.junit.Test;

import de.jmda.core.mproc.task.AbstractTypeElementsTaskTypes;
import de.jmda.core.mproc.task.TaskException;
import de.jmda.core.mproc.task.TaskRunner;
import de.jmda.core.util.StringUtil;

/**
 * Tests task behaviour with transitive dependencies beyond compiler classpath.
 *
 * @author roger@jmda.de
 */
public class JUTBehaviourWithTransitiveDependencies
{
	private class Task extends AbstractTypeElementsTaskTypes
	{
		public Task(Set<? extends Class<?>> types)
    {
	    super(types);
    }

		@Override
		public boolean execute() throws TaskException
		{
			for (TypeElement typeElement : getTypeElements())
			{
				recurse(typeElement, 0);
			}

			return false;
		}
		
		private void recurse(TypeElement typeElement, int depth)
		{
			System.out.println(StringUtil.indent(typeElement.asType().toString(), " ", depth));
			
			for (Element element : typeElement.getEnclosedElements())
			{
				TypeElement innerTypeElement = asTypeElement(element.asType());
				
				if (innerTypeElement == null)
				{
					System.out.println(StringUtil.indent("- " + element.getSimpleName(), " ", depth));
				}
				else
				{
					recurse(innerTypeElement, depth++);
				}
			}
		}
	}

	@Test
	public void shortClasspath() throws IOException
	{
//		Task task = new Task(asSet(JMDASample1.class));
		Task task = new Task(asSet(IDN.class));
//		Task task = new Task(asSet(Dialog.class));
//		Task task = new Task(asSet(StringPrep.class));

		TaskRunner.run(task);
	}
}